'use strict'
import { warn } from './log'
const isNumber = (num: any) => {
  if (typeof num === 'number') {
    return num - num === 0
  }
  if (typeof num === 'string' && num.trim() !== '') {
    return Number.isFinite ? Number.isFinite(+num) : isFinite(+num)
  }
  return false
}

const isOdd = (value: any) => {
  const _value = Math.abs(value)
  if (!isNumber(_value)) throw new TypeError('请输入数字格式数据')
  if (!Number.isInteger(_value)) {
    throw new Error('请输入整数')
  }
  if (!Number.isSafeInteger(_value)) {
    throw new Error('整数太大了，不推荐')
  }
  return _value % 2 === 1
}
// 这种算法始终无法获得最大值，所以需要大一位
const RandomInt = (min: number, max: number) => {
  let num = Math.random() * max + min
  return num | 0
}
const NumberUtils = {
  isNumber,
  isOdd,
  RandomInt,
  format_autofill: (value: string | number, length: number) => {
    if (typeof value == 'string') {
      value = Number.parseInt(value)
    }
    if (value.toString().length > length) {
      return value
    } else {
      let res = ''
      for (let i = 1; i <= length - value.toString().length; i++) {
        res = res + '0'
      }
      return res + value
    }
  }
}
export { isNumber, isOdd, RandomInt, NumberUtils }
